// package/pages/feedBasckDetail/feedBasckDetail.js
const utils = require('../../../utils/util.js');
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let login = require('../../../common/script/login');
let wxbarcode = require('../../../utils/wxbarcode/index.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    domain: api.http,
    seriesArr: [1, 2, 3, 4, 5],
    seriesSum: 0, //卖家服务
    seriesText: '',
    seriesName: '请选择',
    seriesBg: '#D81E06',
    id: '',
    labelData: [],
    starDesc: {},
    serviceLabel: [],
    seriesLabelNum: null,
    seriesSon: [],
    seriesSonNum: [],
    content: '',
    phone: "",
    addImg: [],
    imgList: [],
    addImgBtn: false,
    imgSum: 3,
  },
  //获取输入内容
  bindContent(e) {
    const that = this;
    that.setData({
      content: e.detail.value
    })
  },
  bindPhone(e) {
    const that = this;
    that.setData({
      phone: e.detail.value
    })
  },
  // 添加图片
  addImg() {
    const that = this;
    if (that.data.addImg.length >= 3) {
      wx.showToast({
        title: '图片限制3张',
        icon: 'error',
        duration: 2000
      })
    } else {
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths[0]
          console.log(res)
          wx.showLoading({
            title: '上传中...',
          })
          wx.uploadFile({
            filePath: tempFilePaths,
            name: 'comment_img_file',
            url: api.reqUrls.uploadFile,
            method: "post",
            success(porp) {
              wx.hideLoading();
              let res = JSON.parse(porp.data)
              if (res.status == 1) {
                let imgList = that.data.addImg;
                imgList.push(res.result)
                that.setData({
                  addImg: imgList,
                  addImgBtn: imgList.length >= 3 ? true : false
                })
              } else {
                wx.hideLoading();
                wx.showModal({
                  showCancel: false,
                  content: res.msg,
                  success(res) {
                    if (res.confirm) {

                    }
                  }
                })
              }


            },
            fail(err) {
              wx.hideLoading();
              wx.showToast({
                title: '图片上传失败',
                icon: 'error',
                duration: 2000
              })
            }
          })
        }
      })
    }

  },

  // 删除图片
  removeImg(e) {
    const that = this;
    let imgUrl = that.data.addImg;
    let index = e.currentTarget.dataset.index;
    imgUrl.splice(index, 1);
    that.setData({
      addImg: imgUrl,
      addImgBtn: false,
    })

  },
  // 点击星星评价
  seriesStarClick(e) {
    const that = this;
    let sum = e.currentTarget.dataset.index + 1;
    that.setData({
      seriesSum: sum,
      serviceLabel: [],
      seriesLabelNum: null,
      seriesSon: [],
      seriesSonNum: [],

    })
    that.select(sum);
  },
  select(sum) {
    const that = this;
    let serviceLabel = []
    that.data.labelData.forEach(res => {
      res.xing.forEach(item => {
        if (item == sum) {
          serviceLabel.push(res)
        }
      })
    })

    that.setData({
      seriesName: that.data.starDesc[sum].desc1,
      seriesText: that.data.starDesc[sum].desc2,
      serviceLabel: serviceLabel,
    })
  },
  seriesLabe(e) {
    const that = this;
    let index = e.target.dataset.index;
    let seriesSonNumList = []
    that.data.labelData[index].son.forEach(res => {
      seriesSonNumList.push(false)
    })
    that.setData({
      seriesLabelNum: index,
      seriesSon: that.data.labelData[index].son,
      seriesSonNum: seriesSonNumList
    })
  },
  seriesSon(e) {
    const that = this;
    let index = e.target.dataset.index;
    let seriesSonNumList = that.data.seriesSonNum
    seriesSonNumList[index] = !seriesSonNumList[index]
    let seriesSonText = 0
    seriesSonNumList.forEach(res => {
      if (res) {
        seriesSonText += 1
      }
    })
    if (seriesSonText > 3) {
      seriesSonNumList[index] = !seriesSonNumList[index]
      wx.showToast({
        title: '可选3个子标签',
        icon: 'error',
        duration: 2000
      })
    } else {
      that.setData({
        seriesSonNum: seriesSonNumList
      })
    }
  },
  getMember() {
    const that = this;
    let data = {
      token: wx.getStorageSync('token'),
    }
    myRequest.EasyHttp.post(api.reqUrls.membercardShow, data).then(res => {
      if (res.data.status == 1) {
        that.setData({
          phone: res.data.result.member_data.phone,
        })
      }
    })
  },
  getData() {
    const that = this;
    let data = {
      id: that.data.id
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.feedbackDetail, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        that.setData({
          labelData: res.data.data.detail.label || [],
          starDesc: res.data.data.star_desc,

        })
      } else {
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(e) {}
        })
      }
    })
  },
  submit() {
    const that = this;
    if (!that.data.seriesSum) {
      wx.showToast({
        title: '请选择星评价',
        icon: 'error',
        duration: 2000
      })
      return
    }
    if (that.data.seriesLabelNum == null && that.data.serviceLabel.length > 0) {
      wx.showToast({
        title: '请选择标签',
        icon: 'error',
        duration: 2000
      })
      return
    }
    let son = []
    that.data.seriesSonNum.forEach((res, index) => {
      if (res) {
        son.push(that.data.seriesSon[index])
      }
    })
    if (son.length == 0 && that.data.seriesSon.length > 0) {
      wx.showToast({
        title: '请选择子标签',
        icon: 'error',
        duration: 2000
      })
      return
    }
    if (!that.data.content) {
      wx.showToast({
        title: '请输入意见',
        icon: 'error',
        duration: 2000
      })
      return
    }
    if (!that.data.phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'error',
        duration: 2000
      })
      return
    }
    let labelDate = {
      son: son,
      name: that.data.serviceLabel[that.data.seriesLabelNum].name
    }
    let data = {
      token: wx.getStorageSync('token'),
      label: JSON.stringify(labelDate),
      xing: that.data.seriesSum,
      content: that.data.content,
      picture: JSON.stringify(that.data.addImg),
      phone: that.data.phone,
      org_code: that.data.orgcode,
      feed_back_item_id: that.data.id
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.feedbackAdd, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/index/index',
          })
        }, 2000)
      } else {
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(e) {}
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    that.setData({
      id: options.id,
      orgcode: options.orgcode,
    })
    that.getMember()
    that.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})